﻿using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /*
     * This control can easily help a user displaying GeoObject on its container.
     * Tools can be affected to the control, each tool "knows" what to do with the control when sending it the mouse argument.
     * The redraw method draw the object according to their layer visibility, and level
     */

    public partial class BaseGeoDisplayBox : Control
    {
        #region Fields
        /// <summary>
        /// The tool assigned to the right clic
        /// </summary>
        public BaseTool RightClicTool;
        /// <summary>
        /// The tool assigned to the left clic
        /// </summary>
        public BaseTool LeftClicTool;
        /// <summary>
        /// A container to display objects and with some navigation tools
        /// </summary>
        public BaseViewport Viewport;

        #endregion

        #region Constructor
        /// <summary>
        /// A container to display objects and with some navigation tools
        /// </summary>
        public BaseGeoDisplayBox()
        {
            InitializeComponent();
            
            LeftClicTool = new TNone();
            RightClicTool = new TNone();

            MouseDown += new MouseEventHandler(GeoDisplayBox_MouseDown);
            MouseUp += new MouseEventHandler(GeoDisplayBox_MouseUp);
            MouseMove += new MouseEventHandler(GeoDisplayBox_MouseMove);
        }

        
        #endregion

        #region Events
        void GeoDisplayBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                LeftClicTool.MouseDown(e);
            }
            else if (e.Button == MouseButtons.Right)
            {
                RightClicTool.MouseDown(e);
            }
        }

        void GeoDisplayBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                LeftClicTool.MouseUp(e);
            }
            else if (e.Button == MouseButtons.Right)
            {
                RightClicTool.MouseUp(e);
            }
        }

        void GeoDisplayBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                LeftClicTool.MouseMove(e);
            }
            else if (e.Button == MouseButtons.Right)
            {
                RightClicTool.MouseMove(e);
            }
            else
            {
                LeftClicTool.MouseMove(e);
                RightClicTool.MouseMove(e);
            }
        }
        
        #endregion

        #region Methods
        /// <summary>
        /// The redraw event
        /// </summary>
        protected virtual void Redraw()
        {
            if (Viewport != null)
            {
                Viewport.ClearScene();
                foreach(Layer l in Viewport.Layers)
                {
                    foreach (BaseDrawableObject obj in Viewport.GeoObjects)
                    {
                        if (obj.Layer == l)
                        {
                            if (obj.Layer.Visible)
                            {
                                obj.Draw(Viewport);
                            }
                        }
                    }
                }
                Viewport.DisplayScene();
            }
        }

        /// <summary>
        /// The painting event
        /// </summary>
        protected override void OnPaint(PaintEventArgs pe)
        {
            Redraw();
            LeftClicTool.Draw();
            RightClicTool.Draw();
        } 
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the double buffer of the display
        /// </summary>
        new public bool DoubleBuffered
        {
            get
            {
                return base.DoubleBuffered;
            }
            set
            {
                base.DoubleBuffered = value;
            }
        } 
        #endregion
    }
}
